package com.xxl.job.kettle.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xxl.job.kettle.core.entity.DiCategory;
import com.xxl.job.kettle.core.entity.KRepository;
import com.xxl.job.kettle.core.entity.TreeDTO;
import com.xxl.job.kettle.core.mapper.DiCategoryMapper;
import com.xxl.job.kettle.core.mapper.KRepositoryMapper;
import com.xxl.job.kettle.core.service.DiCategoryService;
import com.xxl.job.kettle.result.Result;
import com.xxl.job.kettle.util.RepositoryUtil;
import lombok.RequiredArgsConstructor;
import org.pentaho.di.repository.AbstractRepository;
import org.pentaho.di.repository.LongObjectId;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 实现功能：
 *
 * @author 作者[zk]
 * @version 1.0.00  2022-11-29 10:06
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DiCategoryServiceImpl extends BaseServiceImpl<DiCategoryMapper, DiCategory> implements DiCategoryService {

    private final KRepositoryMapper kRepositoryMapper;
    @Value("${spoon.url}")
    private String REPOSITORY_URL;

    @Override
    public List<TreeDTO<String>> findDiCategoryByRep(String repId) throws Exception {
        List<TreeDTO<String>> treeList = new ArrayList<>();
        AbstractRepository abstractRepository = RepositoryUtil.getAbstractRepository(repId);
        // 获取当前的路径信息
        RepositoryDirectoryInterface rdi = abstractRepository.loadRepositoryDirectoryTree().findDirectory("");
        List<TreeDTO<String>> subdirectories = RepositoryUtil.getSubdirectories(new ArrayList<>(), rdi);

        treeList.addAll(subdirectories);
        return subdirectories;
    }

    @Override
    public Result<Object> delete(String dirId, String repId) throws Exception {
        Result result = new Result();
        AbstractRepository abstractRepository = RepositoryUtil.getAbstractRepository(repId);
        RepositoryDirectoryInterface directory = abstractRepository.loadRepositoryDirectoryTree().findDirectory(RepositoryUtil.getObjectId(abstractRepository, dirId));
        List<RepositoryDirectoryInterface> children = directory.getChildren();
        if (children.size() == 0) {
            abstractRepository.deleteRepositoryDirectory(directory);
            result.setCode("0000");
            result.setMessage("删除成功！");
        } else {
            result.setCode("9999");
            result.setMessage("当前目录存在下级目录或脚本，不允许删除！");
        }
        return result;
    }

    @Override
    public Result add(Map<String, String> param) throws Exception {
        Result result = new Result();
        AbstractRepository abstractRepository = RepositoryUtil.getAbstractRepository(param.get("repId"));
        RepositoryDirectoryInterface directory;
        if (param.containsKey("dirId") && !StrUtil.isEmpty(param.get("dirId"))) {
            directory = abstractRepository.loadRepositoryDirectoryTree().findDirectory(RepositoryUtil.getObjectId(abstractRepository, param.get("dirId")));
        } else {
            directory = abstractRepository.getUserHomeDirectory();
        }
        RepositoryDirectoryInterface directoryChild = directory.findChild(param.get("name"));
        if (directoryChild == null) {
            abstractRepository.createRepositoryDirectory(directory, param.get("name"));
            result.setCode("200");
            result.setMessage("创建成功！");
        } else {
            result.setCode("500");
            result.setMessage("目录已存在！");
        }
        return result;
    }

    @Override
    public void update(Map<String, String> param) throws Exception {
        AbstractRepository abstractRepository = RepositoryUtil.getAbstractRepository(param.get("repId"));
        RepositoryDirectoryInterface directory = abstractRepository.loadRepositoryDirectoryTree().findDirectory(RepositoryUtil.getObjectId(abstractRepository, Long.valueOf(param.get("dirId"))));
        abstractRepository.renameRepositoryDirectory(new LongObjectId(Long.valueOf(param.get("dirId"))), directory.getParent(), param.get("name"));
    }

    @Override
    public KRepository findRepositoryById(String id) {
        KRepository kRepository = kRepositoryMapper.selectById(id);
        kRepository.setUrl(REPOSITORY_URL);
        return kRepository;
    }
}
